package bdz.projects.sc.support
{
	import bdz.projects.sc.events.SCEvent;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.*;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class SCStartSelection extends MovieClip
	{
		private var terran:MovieClip;
		private var protoss:MovieClip;
		private var zerg:MovieClip;
		private var randrace:MovieClip;
		private var background:MovieClip;
		private var howto:DisplayObject;
		private var choose:MovieClip;
		private var lastSelected:MovieClip;
		public function SCStartSelection()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function assignStageClips():void{
			
			
			terran = this["t"];
			protoss = this["p"];
			zerg = this["zr"];
			randrace = this["r"];
			background = this["bg"];
			howto = this["ht"];
			choose = this["ch"];
			
			
			
			
			terran.buttonMode = protoss.buttonMode = zerg.buttonMode = randrace.buttonMode = true;
			terran.useHandCursor = protoss.useHandCursor = zerg.useHandCursor = randrace.useHandCursor = true;
			
			terran.addEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
			terran.addEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
			
			protoss.addEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
			protoss.addEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
			
			zerg.addEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
			zerg.addEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
			
			randrace.addEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
			randrace.addEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
			
			terran.alpha = protoss.alpha = zerg.alpha = randrace.alpha = 0;
			background.alpha = howto.alpha = choose.alpha = 0;
			howto["ht"].addEventListener(MouseEvent.CLICK, onHowToClick);
			
			TweenMax.to(terran, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			TweenMax.to(zerg, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			TweenMax.to(protoss, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			TweenMax.to(randrace, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			
			
			TweenMax.to(background, .01, {scaleX:.8, scaleY:.8, colorMatrixFilter:{colorize:0xffffff, amount:1}});
			TweenMax.to(howto, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			TweenMax.to(choose, .01, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
			
			
			terran.addEventListener(MouseEvent.CLICK, onRaceClick);
			
			protoss.addEventListener(MouseEvent.CLICK, onRaceClick);
			
			zerg.addEventListener(MouseEvent.CLICK, onRaceClick);
			
			randrace.addEventListener(MouseEvent.CLICK, onRaceClick);
			
			
			transitionIn();
		}
		private function onRaceClick(evt:MouseEvent):void{
			if(lastSelected){
				TweenMax.to(lastSelected, 1, {glowFilter:{color:0x91e600, alpha:0, blurX:0, blurY:0}, ease:Strong.easeOut, colorMatrixFilter:{colorize:0x00ff00, amount:0}});
				lastSelected.addEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
				lastSelected.addEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
				lastSelected.addEventListener(MouseEvent.CLICK, onRaceClick);
			}
			
			TweenMax.to(evt.target, 1, {glowFilter:{color:0x91e600, alpha:1, blurX:20, blurY:20}, ease:Strong.easeOut, colorMatrixFilter:{colorize:0x00ff00, amount:1}});
			lastSelected = evt.target as MovieClip;
			lastSelected.removeEventListener(MouseEvent.ROLL_OVER, onButtonRolls);
			lastSelected.removeEventListener(MouseEvent.ROLL_OUT, onButtonRolls);
			lastSelected.removeEventListener(MouseEvent.CLICK, onRaceClick);
			dispatchEvent(new SCEvent(SCEvent.RACE_SELECT, evt.target.name));
		}
		private function transitionIn():void{
			var delay:Number = .5;
			TweenMax.to(background, .9, {ease:Strong.easeInOut, delay:delay, alpha:1, scaleX:1, scaleY:1.28, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .5;
			TweenMax.to(choose, .6, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .1;
			TweenMax.to(terran, .8, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .1;
			TweenMax.to(protoss, .8, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .1;
			TweenMax.to(zerg, .8, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .1;
			TweenMax.to(randrace, .8, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
			delay += .1;
			TweenMax.to(howto, .01, {alpha:1, delay:delay, colorMatrixFilter:{colorize:0xffffff, amount:0}});
		}
		private function onHowToClick(evt:MouseEvent):void{
			trace("how to click");
		}
		private function onButtonRolls(evt:MouseEvent):void{
			if(evt.type == MouseEvent.ROLL_OVER){
				TweenMax.to(evt.target, 1, {glowFilter:{color:0x91e600, alpha:1, blurX:20, blurY:20}, ease:Strong.easeOut, colorMatrixFilter:{colorize:0x00ff00, amount:1}});
			}else{
				TweenMax.to(evt.target, 1, {glowFilter:{color:0x91e600, alpha:0, blurX:0, blurY:0}, ease:Strong.easeOut, colorMatrixFilter:{colorize:0x00ff00, amount:0}});
			}
		}
		private function initializeComponent(evt:Event):void{
			assignStageClips();	
		}
	}
}